﻿using AntaresShell.BaseClasses;
using Repository.MODELs;
using Repository.Repositories;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Antares.VIEWMODELs
{
    public class TaskLogViewModel : ViewModelBase
    {
        private ObservableCollection<TaskLogModel> _listItems;
        public ObservableCollection<TaskLogModel> ListItems
        {
            get { return _listItems; }
            set { SetProperty(ref _listItems, value); }
        }

        public TaskLogViewModel(int id)
        {
            BindingData(id);
        }

        private async void BindingData(int taskid)
        {
            var logs = await TaskLogRepository.Instance.GetLogForTask(taskid);

            if (logs == null)
            {
                return;
            }

            var ci = new CultureInfo("en-US");
            foreach (var item in logs)
            {
                try
                {
                    var date = DateTime.Parse(item.DateTaken, ci);
                    item.DateTaken = date.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern) + " " + date.ToString(CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern);
                }
                catch { }
                try
                {
                    var user = await UserInformationRepository.Instance.GetUser(item.UserID);
                    item.Username = user.Username;
                }
                catch { }
            }

            ListItems = logs;
        }
    }
}
